import { addDishAPI, deleteDishAPI, fetchDishByIdAPI, fetchDishDetailAPI, fetchDishListAPI, fetchDishOrStemCategoryAPI, updateDishAPI, updateDishDetailAPI, updateDishStatusAPI } from '@/api/dishAPI'

const actions = {
  /* 获取菜品分类或者套餐分类的action */
  async fetchDishOrStemCategoryAction(context, type) {
    const { data } = await fetchDishOrStemCategoryAPI(type)
    return data
  },
  /* 获取菜品列表的action */
  async fetchDishListAction(context, params) {
    const { data } = await fetchDishListAPI(params)
    context.commit('setDishListMutation', data)
  },

  /* 起售和停售菜品的action */
  async updateDishStatusAction(context, { status, id }) {
    await updateDishStatusAPI(status, id)
    return status === 1 ? '启售成功' : '停售成功'
  },
  /* 删除菜品的action */
  async deleteDishAction(context, id) {
    await deleteDishAPI(id)
    return '删除菜品成功'
  },
  /* 添加菜品的action */
  async addDishAction(context, formData) {
    await addDishAPI(formData)
    return '添加菜品成功'
  },
  /* 查询菜品的action */
  async fetchDishDetailAction(context, id) {
    const { data } = await fetchDishDetailAPI(id)
    return data
  },
  /* 修改菜品的action */
  async updateDishAction(context, dish) {
    await updateDishAPI(dish)
  },
  /* 根据分类id获取菜品 */
  async fetchDishByIdAction(context, id) {
    const { data } = await fetchDishByIdAPI(id)
    return data
  }
}

const mutations = {
  /* 设置菜品列表和菜品总数的mutation */
  setDishListMutation(state, { total, records }) {
    state.records = records
    state.total = total
  }
}

const state = {
  records: [],
  total: 0
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
